package com.zkzx.operation.api.controller.company;

import com.zkzx.common.company.command.CompanyCommand;
import com.zkzx.common.company.param.CompanyFilterParam;
import com.zkzx.common.vo.PageResult;
import com.zkzx.common.vo.RequestFilterParams;
import com.zkzx.common.vo.StatusVO;
import com.zkzx.operation.api.client.company.CompanyClient;
import com.zkzx.operation.api.controller.BaseController;
import com.zkzx.common.company.model.CompanyListVO;
import com.zkzx.common.company.model.CompanyVO;
import com.zkzx.operation.api.mapper.entity.Member;
import com.zkzx.operation.api.service.menu.MemberService;
import com.zkzx.util.json.JSONResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * @author young Email:yanghui@zkraising.com
 * @description
 * @company 北京中科智兴物联科技有限公司 @版权所有
 * @since 2021年04月28日 17时51分
 */
@RestController
@Validated
@Api(tags="企业管理")
@RequestMapping(value = "/companies")
public class CompanyController extends BaseController {

    @Autowired
    private CompanyClient companyClient;

    @Autowired
    private MemberService memberService;

    @GetMapping
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取企业列表", notes = "获取企业列表")
    public PageResult<CompanyListVO> find(@ApiParam(value = "查询参数")  CompanyFilterParam filterParam,
                                          @ApiParam(value = "分页查询参数") RequestFilterParams params) {
        return companyClient.find(JSONResultUtil.object2JSON(filterParam),JSONResultUtil.object2JSON(params));
    }

    @GetMapping("/{id}")
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取企业详细", notes = "获取企业详细")
    public CompanyVO getById(@PathVariable("id") Integer id) {
        // 获取企业详细
        CompanyVO companyVO = companyClient.getById(id);
        // 加载密码
        Member member = memberService.findMemberByCompanyId(id);
        if(Objects.nonNull(member)) {
            companyVO.setPwd(member.getPwd());
        }
        return companyVO;
    }

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    @ApiOperation(value = "新增企业信息", notes = "新增企业信息")
    public StatusVO add(@RequestBody @Valid CompanyCommand command) {
        // 新增企业信息
        StatusVO vo = companyClient.add(command, super.getUserId());

        // 新增企业账户
        memberService.add(command, vo.getId(), super.getUserId());
        return vo;
    }

    @PutMapping("/{id}")
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "修改企业信息", notes = "修改企业信息")
    public void update(@PathVariable("id") Integer id,@RequestBody @Valid CompanyCommand command) {
        // 修改企业信息
        companyClient.update(command,id,super.getUserId());

        // 新增企业账户
        memberService.add(command,id,super.getUserId());
    }


    @PutMapping("/{id}/password")
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "修改企业登录密码", notes = "修改企业登录密码")
    public void updatePwd(@PathVariable("id") Integer id) {
        memberService.updatePwd(id,super.getUserId());
    }


    @PostMapping("/{id}")
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "提交企业信息", notes = "提交企业信息")
    public void submit(@PathVariable("id") Integer id) {
        // 提交企业信息
        companyClient.submit(id,super.getUserId());
    }
}
